#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#




import wsgiref.handlers
import time
import random



from google.appengine.ext import webapp

class BasePage(webapp.RequestHandler):

  def google_str(self,no):
	  if no[0] == '6':
		  return 'SHA:' + no
	  return 'SHE:' + no

  def sina_str(self,no):
	  if no[0] == '6':
		  return 'sh' + no
	  return 'sz' + no

  def create_link_str_sina(self,no, all_flag ):
	  v = random.randint( 100000000000,999999999999 )
	  #v = 1234
	  str_v = '%d' % v
	  url_str_time = '"http://3g.sina.com.cn/3g/static/images/finance/stock/daily2/' + self.sina_str(no) + '.gif?r=' + str_v + '"'
	  url_str_day = '"http://3g.sina.com.cn/3g/static/images/finance/stock/k/daily2/' + self.sina_str(no) + '.gif?r=' + str_v + '"'
	  url_str_week = '"http://3g.sina.com.cn/3g/static/images/finance/stock/k/week2/' + self.sina_str(no) + '.gif?r=' + str_v + '"'
	  url_str_month = '"http://3g.sina.com.cn/3g/static/images/finance/stock/k/month2/' + self.sina_str(no) + '.gif?r=' + str_v + '"'
	  str = no
	  str += ' <img title="time" src=' + url_str_time + ' alt="time" />\n'
	  if( all_flag == 1 ):
	  	str += ' <img title="day" src=' + url_str_day + ' alt="day" />\n'
	  	str += ' <img title="week" src=' + url_str_week + ' alt="week" />\n'
	  	str += ' <img title="month" src=' + url_str_month + ' alt="month" />\n'
	  str += '<p>'
	  return str

  def create_link_str_google_rate(self,no):
	  str = self.google_str(no) + '<img alt="http://www.google.cn/finance/chart?q=' + self.google_str(no) + '&tlf=24&ebp=1" src="http://www.google.cn/finance/chart?q=' + no + '&tlf=24&ebp=1"></img> '
	  return str

  def create_link_str_google_3_day(self,no):
	  c,n = no.split(":")
	  str = no + '<img src="http://www.google.cn/finance/getchart?q=' + n + '&x=' + c + '&p=1d&i=240"></img>'
	  return str

  def create_link_str(self,no, all_flag):
  	return self.create_link_str_sina(no, all_flag)
  	#return self.create_link_str_google_rate(no)
  	#return self.create_link_str_google_3_day(no)

  def get(self, all_flag=0 ):
	like_no = ( '000033','600006','600778','600133','000099','000002','600886' );
	big_no = ( '601857' ,'601398' ,'600028' ,'601988' ,'601088' ,'601628' ,'600036' ,'601318' ,'601328' ,'601601' ,'600019' ,'601919' ,'601600' ,'601006' ,'601998' ,'600050' ,'600030' ,'601898' ,'600519' ,'600016' ,'601166' ,'000002' ,'600000' ,'600018' ,'600900' ,'600837' ,'601390' ,'600005' ,'601186' ,'000898' ,'601111' ,'601991' ,'000858' );
	for i in range( 0, len(like_no) ):
		#self.response.out.write( '<p>' )
		str = self.create_link_str( like_no[i], all_flag )
    		self.response.out.write( str )
    	self.response.out.write('<p><p><p>Big Stock<p>\n')
	for i in range( 0, len(big_no) ):
		#self.response.out.write( '<p>' )
		str = self.create_link_str( big_no[i], all_flag )
    		self.response.out.write( str )


class AllHandler(BasePage):
	def get( self ):
		BasePage.get(self, 1)

class MainHandler(BasePage):
  	def get( self ):
		BasePage.get(self)


def main():
  application = webapp.WSGIApplication([
	  ('/', MainHandler)
	  ,('/all', AllHandler)
	  ], debug=True)
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
	main()
